#!/usr/bin/env node
/**
 * 检查服务状态的脚本
 */

const { exec } = require('child_process');

function checkService(serviceName, command) {
  return new Promise((resolve) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
        console.log(`❌ ${serviceName}: 未运行`);
        resolve(false);
      } else {
        console.log(`✅ ${serviceName}: 正在运行`);
        resolve(true);
      }
    });
  });
}

async function checkServices() {
  console.log('🔍 检查服务状态...\n');
  
  // 检查后端服务
  const backendRunning = await checkService(
    '后端服务 (端口8001)', 
    'netstat -an | findstr :8001'
  );
  
  // 检查前端服务  
  const frontendRunning = await checkService(
    '前端服务 (端口8002)',
    'netstat -an | findstr :8002'
  );
  
  console.log('\n📋 服务状态总结:');
  console.log(`后端服务: ${backendRunning ? '✅ 运行中' : '❌ 未运行'}`);
  console.log(`前端服务: ${frontendRunning ? '✅ 运行中' : '❌ 未运行'}`);
  
  if (!backendRunning) {
    console.log('\n💡 建议: 请先启动后端服务:');
    console.log('   python -m uvicorn main:app --host 0.0.0.0 --port 8001');
  }
  
  if (!frontendRunning) {
    console.log('\n💡 建议: 请启动前端服务:');
    console.log('   cd client && npm run dev -- --port 8002');
  }
  
  if (backendRunning && frontendRunning) {
    console.log('\n🎉 所有服务正常运行！');
    console.log('前端: http://localhost:8002');
    console.log('后端: http://localhost:8001');
  }
}

checkServices().catch(console.error);